all files / lib/ group.js

97.56% Statements 40/41
90.48% Branches 19/21
100% Functions 6/6
100% Lines 36/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                                    15×                                                                                   17× 17× 17×   17×   17×     17×                             22×              
'use strict';
 
/**
 * Module dependencies.
 */
 
var inherit = require('./utils').inherit;
var isEmail = require('is-email');
var newDate = require('new-date');
var Facade = require('./facade');
 
/**
 * Initialize a new `Group` facade with a `dictionary` of arguments.
 *
 * @param {Object} dictionary
 *   @param {string} userId
 *   @param {string} groupId
 *   @param {Object} properties
 *   @param {Object} options
 * @param {Object} opts
 *   @property {boolean|undefined} clone
 */
function Group(dictionary, opts) {
  Facade.call(this, dictionary, opts);
}
 
/**
 * Inherit from `Facade`
 */
 
inherit(Group, Facade);
 
/**
 * Get the facade's action.
 */
Group.prototype.action = function() {
  return 'group';
};
 
Group.prototype.type = Group.prototype.action;
 
/**
 * Setup some basic proxies.
 */
Group.prototype.groupId = Facade.field('groupId');
 
/**
 * Get created or createdAt.
 *
 * @return {Date}
 */
Group.prototype.created = function() {
  var created = this.proxy('traits.createdAt')
    || this.proxy('traits.created')
    || this.proxy('properties.createdAt')
    || this.proxy('properties.created');
 
  if (created) return newDate(created);
};
 
/**
 * Get the group's email, falling back to the group ID if it's a valid email.
 *
 * @return {string}
 */
Group.prototype.email = function() {
  var email = this.proxy('traits.email');
  if (email) return email;
  var groupId = this.groupId();
  if (isEmail(groupId)) return groupId;
};
 
/**
 * Get the group's traits.
 *
 * @param {Object} aliases
 * @return {Object}
 */
Group.prototype.traits = function(aliases) {
  var ret = this.properties();
  var id = this.groupId();
  aliases = aliases || {};
 
  if (id) ret.id = id;
 
  for (var alias in aliases) {
    var value = this[alias] == null ? this.proxy('traits.' + alias) : this[alias]();
    Iif (value == null) continue;
    ret[aliases[alias]] = value;
    delete ret[alias];
  }
 
  return ret;
};
 
/**
 * Special traits.
 */
 
Group.prototype.name = Facade.proxy('traits.name');
Group.prototype.industry = Facade.proxy('traits.industry');
Group.prototype.employees = Facade.proxy('traits.employees');
 
/**
 * Get traits or properties.
 *
 * TODO: remove me
 *
 * @return {Object}
 */
Group.prototype.properties = function() {
  return this.field('traits') || this.field('properties') || {};
};
 
/**
 * Exports.
 */
 
module.exports = Group;